package com.kakarote.examine.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.kakarote.core.feign.admin.entity.AdminMessageBO;
import com.kakarote.core.feign.admin.entity.AdminMessageEnum;
import com.kakarote.core.feign.examine.entity.ExamineFlowFinalUser;
import com.kakarote.core.feign.examine.entity.ExamineFlowFinalVO;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.examine.constant.ExamineStatusEnum;
import com.kakarote.examine.entity.PO.ExamineFlowFinal;
import com.kakarote.examine.entity.PO.ExamineFlowFinalOptional;
import com.kakarote.examine.entity.PO.ExamineRecordLog;
import com.kakarote.examine.mapper.ExamineFlowFinalMapper;
import com.kakarote.examine.service.IExamineFlowFinalOptionalService;
import com.kakarote.examine.service.IExamineFlowFinalService;
import com.kakarote.examine.service.IExamineRecordLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 提交审批流程表-最终流程 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2022-08-31
 */
@Service
public class ExamineFlowFinalServiceImpl extends BaseServiceImpl<ExamineFlowFinalMapper, ExamineFlowFinal> implements IExamineFlowFinalService {

    @Autowired
    private IExamineFlowFinalOptionalService examineFlowFinalOptionalService;

    @Autowired
    private IExamineRecordLogService examineRecordLogService;


    @Override
    public Integer getMaxSort(Long recordId, Long examineId) {
        return getBaseMapper().getMaxSort(recordId, examineId);
    }

    @Override
    public AdminMessageBO setCopyMessageType(AdminMessageBO adminMessageBO, Integer label) {
        //审批label枚举 1 合同 2 回款 3发票 4薪资 5 采购审核 6采购退货审核 7销售审核 8 销售退货审核 9付款单审核10 回款单审核11盘点审核12调拨审核
        if (0 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.OA_EXAMINE_COPY.getType());
        }
        if (1 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.CRM_CONTRACT_COPY.getType());
        }
        if (2 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.CRM_RECEIVABLES_COPY.getType());
        }
        if (3 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.CRM_INVOICE_COPY.getType());
        }
        if (4 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.HRM_SALARY_COPY.getType());
        }
        if (5 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_PURCHASE_COPY.getType());
        }
        if (6 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_RETREAT_COPY.getType());
        }
        if (7 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_SALE_COPY.getType());
        }
        if (8 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_SALE_RETURN_COPY.getType());
        }
        if (9 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_PAYMENT_COPY.getType());
        }
        if (10 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_COLLECTION_COPY.getType());
        }
        if (11 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_INVENTORY_COPY.getType());
        }
        if (12 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.JXC_ALLOCATION_COPY.getType());
        }
        if (20 == label) {
            adminMessageBO.setMessageType(AdminMessageEnum.CRM_FLOW_COPY.getType());
        }

        return adminMessageBO;
    }

    @Override
    public List<ExamineFlowFinalVO> queryFinalFlowList(Long recordId) {
        List<ExamineFlowFinal> examineFlowFinalList = lambdaQuery().eq(ExamineFlowFinal::getRecordId, recordId).orderByAsc(ExamineFlowFinal::getSort, ExamineFlowFinal::getCreateTime).list();
        List<ExamineFlowFinalVO> examineFlowFinalVOList = new ArrayList<>();

        //获取最终全流程处理人数据列表
        List<ExamineFlowFinalOptional> examineFlowFinalOptionalList = examineFlowFinalOptionalService.lambdaQuery()
                .select(ExamineFlowFinalOptional::getFlowId, ExamineFlowFinalOptional::getUserId, ExamineFlowFinalOptional::getEmail, ExamineFlowFinalOptional::getSort)
                .eq(ExamineFlowFinalOptional::getRecordId, recordId)
                .orderByAsc(ExamineFlowFinalOptional::getCreateTime, ExamineFlowFinalOptional::getSort)
                .list();

        if (CollectionUtil.isNotEmpty(examineFlowFinalList)) {
            Map<Long, List<ExamineFlowFinalOptional>> examineFlowFinalOptionalUserList = examineFlowFinalOptionalList.stream().collect(Collectors.groupingBy(ExamineFlowFinalOptional::getFlowId));

            List<ExamineRecordLog> recordLogs = examineRecordLogService.lambdaQuery()
                    .eq(ExamineRecordLog::getRecordId, recordId)
                    .eq(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus())
                    .orderByAsc(ExamineRecordLog::getCreateTime)
                    .list();

            Map<Long, List<ExamineRecordLog>> longListMap = recordLogs.stream().collect(Collectors.groupingBy(ExamineRecordLog::getFlowId));

            examineFlowFinalList.forEach(
                    examineFlowFinal -> {
                        ExamineFlowFinalVO examineFlowFinalVO = new ExamineFlowFinalVO();
                        BeanUtil.copyProperties(examineFlowFinal, examineFlowFinalVO);
                        List<ExamineFlowFinalOptional> examineFlowFinalOptionalOfFlow = examineFlowFinalOptionalUserList.get(examineFlowFinal.getFlowId());
                        if (CollectionUtil.isNotEmpty(examineFlowFinalOptionalOfFlow)) {
                            List<ExamineFlowFinalUser> userList = examineFlowFinalOptionalOfFlow.stream().map(item -> {
                                ExamineFlowFinalUser finalUser = new ExamineFlowFinalUser();
                                finalUser.setUserId(item.getUserId());
                                finalUser.setEmail(item.getEmail());
                                return finalUser;
                            }).collect(Collectors.toList());
                            examineFlowFinalVO.setUserList(userList);
                        } else {
                            List<ExamineRecordLog> recordLogs1 = longListMap.get(examineFlowFinal.getFlowId());
                            if (CollectionUtil.isNotEmpty(recordLogs1)) {
                                List<ExamineFlowFinalUser> userList = recordLogs.stream().map(item -> {
                                    ExamineFlowFinalUser finalUser = new ExamineFlowFinalUser();
                                    finalUser.setUserId(item.getExamineUserId());
                                    finalUser.setEmail(item.getExamineUserEmail());
                                    return finalUser;
                                }).collect(Collectors.toList());
                                examineFlowFinalVO.setUserList(userList);
                            }
                        }
                        examineFlowFinalVOList.add(examineFlowFinalVO);

                    }
            );
        }
        return examineFlowFinalVOList;
    }


}
